package com.liutaw.nanjingair.views.fragments;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.dagger.component.BaseWeatherComponent;
import com.liutaw.nanjingair.presenter.IndexPresenterImpl;
import com.liutaw.nanjingair.views.activities.AndroidApplication;
import com.liutaw.nanjingair.views.adapter.WeatherHourInfoAdapter;
import com.liutaw.nanjingair.views.viewcomponent.DividerLine;
import com.liutaw.nanjingair.views.viewcomponent.WrappingLinearLayoutManager;
import com.liutaw.nanjingair.views.viewcomponent.weather.WeatherReport;
import com.liutaw.navigationbarlib.NaviBarBuilder;
import com.szjlxh.domain.model.SunInfo;
import com.szjlxh.domain.model.index.WeatherBaseData;
import com.szjlxh.domain.model.index.WeatherHourData;
import com.szjlxh.domain.model.index.WeatherSevenDay;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class IndexFragment extends BaseFragment implements IIndexFragment {
    @Bind(R.id.navi_container)
    FrameLayout navi_container;
    NaviBarBuilder navigationBarView;
    @Bind(R.id.recycleview_future_hour)
    RecyclerView recycleview_future_hour;
    @Bind(R.id.recycleview_before_hour)
    RecyclerView recycleview_before_hour;
    @Bind(R.id.base_weather_info)
    TextView baseWeatherInfo;
    @Bind(R.id.base_wind_direction)
    TextView baseWindDirection;
    @Bind(R.id.base_weather_humidity)
    TextView baseWeatherHumidity;
    @Bind(R.id.base_weather_pm)
    TextView baseWeatherPm;
    @Bind(R.id.base_weather_visibility)
    TextView baseWeatherVisibility;
    @Bind(R.id.base_weather_human_temp)
    TextView baseWeatherHumanTemp;
    @Bind(R.id.base_weather_update_date)
    TextView baseWeatherUpdateDate;
    @Bind(R.id.base_text_date)
    TextView baseTextDate;
    @Bind(R.id.view_degree)
    View view_degree;
    private WeatherHourInfoAdapter future24HoursAdapter;
    private WeatherHourInfoAdapter last24HoursAdapter;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.weather_report)
    WeatherReport weather_report;
    @Inject
    IndexPresenterImpl indexPresenterImpl;
    @Bind(R.id.text_sunrise)
    TextView text_sunrise;
    @Bind(R.id.text_sunset)
    TextView text_sunset;
    @Bind(R.id.text_pm_number)
    TextView base_temp;

    public IndexFragment() {
//        indexPresenterImpl = new IndexPresenterImpl(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        injectModule();
        View view = inflater.inflate(R.layout.layout_fragment_index, container, false);
        ButterKnife.bind(this, view);
        navigationBarView = new NaviBarBuilder(getActivity());
        navigationBarView.buildLeftView().buildImg(R.drawable.icon_topbar_left_index).buildCenterView().buildTitleText("定位中");
        navi_container.addView(navigationBarView.build());
        navigationBarView.getWholeView().setBackgroundColor(getResources().getColor(android.R.color.transparent));
        future24HoursAdapter = new WeatherHourInfoAdapter(getContext());
        recycleview_future_hour.setAdapter(future24HoursAdapter);
        last24HoursAdapter = new WeatherHourInfoAdapter(getContext());
        recycleview_before_hour.setAdapter(last24HoursAdapter);


        initRecyclerView(recycleview_future_hour);//未来24小时
        initRecyclerView(recycleview_before_hour);//过去24小时

        scrollView.setFillViewport(true);

        initData();

        return view;
    }

    private void injectModule() {
        getComponent(BaseWeatherComponent.class).inject(this);
        indexPresenterImpl.setIndexFragment(this);
    }

    private void initData() {
//        navigationBarView.showProgressBar();

        indexPresenterImpl.refreshBaseWeaterData();
        indexPresenterImpl.refreshFuture24HoursData();
        indexPresenterImpl.refreshLast24HoursData();
        indexPresenterImpl.refreshSevenDayWeatherData();
        indexPresenterImpl.refreshSunData();
        indexPresenterImpl.refreshStation();
        initChineseDate();
//        indexPresenterCompl.test();
    }

    private void initChineseDate() {
    }


    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager linearLayoutManager = new WrappingLinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        DividerLine dividerLine = new DividerLine(DividerLine.HORIZONTAL);
        dividerLine.setSize(1);
        dividerLine.setColor(0x33FFFFFF);
        recyclerView.addItemDecoration(dividerLine);
    }

    @Override
    public void onRefreshData() {

    }

    @Override
    public void onDestory() {
        super.onDestroy();
        indexPresenterImpl.onDestory();
//        IndexFragment.this.onDestroy();
    }

    @Override
    public void onSevenDayWeatherDataLoaded(List<WeatherSevenDay> weatherSevenDay) {
        weather_report.setWeatherDayInfos(weatherSevenDay);
    }

    @Override
    public void onSenvenDayWeatherDataLoadedFailed(String message) {
        AndroidApplication.getInstance().showToastMessage(message);
    }

    @Override
    public void onFuture24HoursWeatherDataLoaded(List<WeatherHourData> hoursWeatherBean) {
        future24HoursAdapter.setData(hoursWeatherBean);
    }

    @Override
    public void onFuture24HoursWeatherDataLoadedFailed(String message) {
        AndroidApplication.getInstance().showToastMessage(message);
    }

    @Override
    public void onLast24HoursWeatherDataLoaded(List<WeatherHourData> hoursWeatherBean) {
        last24HoursAdapter.setData(hoursWeatherBean);
    }

    @Override
    public void onLast24HoursWeatherDataLoadedFailed(String message) {
        AndroidApplication.getInstance().showToastMessage(message);
    }

    @Override
    public void onSunDataLoaded(final SunInfo sunInfo) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                text_sunrise.setText("日出 " + sunInfo.getDownriseTime());
                text_sunset.setText("日落 " + sunInfo.getDowndownTime());
            }
        });
    }

    @Override
    public void onSunDataFailed(final SunInfo test) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text_sunrise.setText("日出 " + test.getDownriseTime());
                text_sunset.setText("日落 " + test.getDowndownTime());
            }
        });
    }

    @Override
    public void onWeatherBaseLoaded(WeatherBaseData baseData) {

        base_temp.setText(baseData.getTemp());
        baseWeatherInfo.setText(baseData.getWeatherName());
        baseWindDirection.setText(baseData.getWindDirection());
        baseWeatherHumidity.setText(baseData.getHumidity());
        baseWeatherHumanTemp.setText(baseData.getFeelingTemp());
        baseWeatherPm.setText(baseData.getPm25());
        baseWeatherVisibility.setText(baseData.getVisibility());
        baseWeatherUpdateDate.setText(baseData.getUpdateDate());
        baseTextDate.setText(baseData.getCurrentDate());
        view_degree.setVisibility(View.VISIBLE);
    }

//    @Override
//    public void onWeatherBaseLoaded(final WeatherBaseBean baseBean) {
//        if (baseBean == null || baseBean.getResult() == null || baseBean.getResult().size() == 0) {
//            onWeatherBaseLOadedFailed("接口数据为空");
//            return;
//        }
//        getActivity().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                WeatherBaseBean.ResultEntity content = baseBean.getResult().get(0);
//                float tempF = Float.valueOf(content.getTEMP());
//                int tempI = (int) tempF;
//                base_temp.setText(tempI + "");
//                String weather = Constants.getWeatherData().get(content.getWEATHERCODE());
//                if (weather == null)
//                    baseWeatherInfo.setText(content.getWEATHERCODE());
//                else
//                    baseWeatherInfo.setText(weather);
//                baseWindDirection.setText("风向：" + content.getWIND_DIRECTIONNAME());
//                baseWeatherHumidity.setText("湿度：" + content.getHUMIDITY() + "%");
//                baseWeatherHumanTemp.setText("体感温度：" + content.getFEELTEMP() + "℃");
//                baseWeatherPm.setText("PM2.5：" + content.getPM25());
//                baseWeatherVisibility.setText("能见度：" + content.getFormatVisibility());
//                baseWeatherUpdateDate.setText(content.getFormatUpdateDate());
//                baseTextDate.setText(DateUtils.getDate());
//            }
//        });
//    }

    @Override
    public void onWeatherBaseLOadedFailed(final String message) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                base_temp.setText("出错");
//                baseWeatherInfo.setText("阴天");
//                baseWindDirection.setText("显示风向");
//                baseWeatherHumidity.setText("湿度：94%");
//                baseWeatherVisibility.setText("能见度：6km");
//                baseWeatherHumanTemp.setText("体感温度：5℃");
//                baseWeatherPm.setText("PM2.5：53");
                baseWeatherUpdateDate.setText("");
                baseTextDate.setText("当前无实况");
                view_degree.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onStationLoaded(String statinName) {
        navigationBarView.changeTitleContent(statinName);
    }

    @Override
    public void onStationLoadedFailed() {
        navigationBarView.changeTitleContent("无站点");
    }

    @Override
    public Context getContextInstance() {
        return getActivity().getApplicationContext();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
